﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZDSoft.LMS.Core;
using ZDSoft.LMS.Service;
using ZDSoft.LMS.Domain;
using System.Collections;
using ZDSoft.LMS.Web.Apps;
using ZDSoft.LMS.Manager;

namespace ZDSoft.LMS.Web.Controllers
{
    /// <summary>
    /// 货运单管理
    /// </summary>
    public class DeliveryFormController : UcController
    {
        //
        // GET: /DeliveryForm/

        public ActionResult Index(int pageIndex = 1, string transporter = "", string deliveryDate = "", string commisionder = "", string receiver = "")
        {
            int count = 0;//用于存放满足条件的记录总数
            //var listAll = Container.Instance.Resolve<IDeliveryFormService>().DemoSimpleQuery(pageIndex, PagerHelper.PageSize, transporter, deliveryDate, commisionder, receiver);
            IList<QueryConditions> conditions = GetConditions(transporter, deliveryDate, commisionder, receiver);
            var listAll = Container.Instance.Resolve<IDeliveryFormService>().GetPaged(conditions, pageIndex, PagerHelper.PageSize, out count);
            PageList<DeliveryForm> pageList = listAll.ToPageList<DeliveryForm>(pageIndex, PagerHelper.PageSize, count);//转换为PageList集合，用于分页控件显示不同的页码
            return View(pageList);
        }

        private IList<QueryConditions> GetConditions(string transporter, string deliveryDate, string commisionder, string receiver)
        {
            IList<QueryConditions> conditions = new List<QueryConditions>();
            if (!string.IsNullOrEmpty(transporter))
            {
                conditions.Add(new QueryConditions() { PropertyName = "Transporter.UserName", Operator = "like", Value = transporter });
            }
            if (!string.IsNullOrEmpty(deliveryDate))
            {
                conditions.Add(new QueryConditions() { PropertyName = "DeliveryDate", Operator = "=", Value = deliveryDate });
            }
            if (!string.IsNullOrEmpty(commisionder))
            {
                conditions.Add(new QueryConditions() { PropertyName = "TransportOrder.Commisionedor.Name", Operator = "like", Value = commisionder });
            }
            if (!string.IsNullOrEmpty(receiver))
            {
                conditions.Add(new QueryConditions() { PropertyName = "TransportOrder.Receiver.Name", Operator = "like", Value = receiver });
            }
            return conditions;
        }

        //
        // GET: /DeliveryForm/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /DeliveryForm/Create

        public ActionResult Create()
        {
            GetCreateData();
            DeliveryForm df = new DeliveryForm();
            return View(df);
        }

        private void GetCreateData()
        {
            ViewBag.TransportOrder = Container.Instance.Resolve<ITransportOrderService>().GetAll();
            var driverList = Container.Instance.Resolve<IDriverService>().GetAll();
            if (driverList != null)
            {
                driverList = driverList.OrderBy(o => o.UserName).ToList<Driver>();
            }
            ViewData["driverList"] = driverList;

            var vehicleList = Container.Instance.Resolve<IVehicleService>().GetAll();
            if (vehicleList != null)
            {
                vehicleList = vehicleList.OrderBy(o => o.VehicleNumber).ToList<Vehicle>();
            }
            ViewData["VehicleList"] = vehicleList;

            var tranportOrderList = Container.Instance.Resolve<ITransportOrderService>().GetAll();
            if (tranportOrderList != null)
            {
                tranportOrderList = tranportOrderList.OrderBy(o => o.ID).ToList<TransportOrder>();
            }
            ViewData["tranportOrderList"] = tranportOrderList;

            //var dictionaryList = Container.Instance.Resolve<IDictionariesService>().GetByParent("gdzt");
            //if (dictionaryList != null)
            //{
            //    dictionaryList = dictionaryList.OrderBy(o => o.ID).ToList<Dictionaries>();
            //}
            //ViewData["dictionaryList"] = dictionaryList;
        }

        //
        // POST: /DeliveryForm/Create

        [HttpPost]
        public ActionResult Create(DeliveryForm deliveryForm)
        {
            if (ModelState.IsValid)
            {
                Container.Instance.Resolve<IDeliveryFormService>().Create(deliveryForm);
                return RedirectToAction("Index");
            }
            GetCreateData();
            return View();
        }

        //
        // GET: /DeliveryForm/Edit/5

        public ActionResult Edit(int id)
        {
            GetBaseData(id);
            DeliveryForm form = Container.Instance.Resolve<IDeliveryFormService>().Get(id);
            return View(form);
        }

        //
        // POST: /DeliveryForm/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, DeliveryForm deliveryForm)
        {
            if (ModelState.IsValid)
            {
                Container.Instance.Resolve<IDeliveryFormService>().Update(deliveryForm);
            }
            return RedirectToAction("Index");
        }

        private void GetBaseData(int id)
        {
            //ViewBag.ViewSchedule = Container.Instance.Resolve<IViewScheduleService>().Get(id);

            IList<Driver> driverList = Container.Instance.Resolve<IDriverService>().GetAll();
            if (driverList != null)
            {
                driverList = driverList.OrderBy(o => o.UserName).ToList();
            }
            ViewBag.DriverList = driverList;

            IList<Vehicle> vehicleList = Container.Instance.Resolve<IVehicleService>().GetAll();
            if (vehicleList != null)
            {
                vehicleList = vehicleList.OrderBy(o => o.VehicleNumber).ToList();
            }
            ViewBag.VehicleList = vehicleList;
        }


        //
        // GET: /DeliveryForm/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /DeliveryForm/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            return RedirectToAction("Index");
        }
    }
}
